package com.open.party.cloud.view.home.dangYuanJiaoYu

import android.os.Bundle
import com.sinothk.android.utils.inters.OnSuperListener
import cn.android.x.model.data.AppPageData
import cn.android.x.model.data.PageReq
import cn.android.x.model.data.ResultInfo
import cn.android.x.parent.AppEventBusRecycleViewTitleBaseActivity
import com.open.party.cloud.R
import com.open.party.cloud.model.*
import com.open.party.cloud.view.home.dangYuanJiaoYu.adapter.LiangDuYunKeTangListAdapter
import com.open.party.cloud.viewModel.BizViewModel
import com.sinothk.android.utils.XUtils
import com.sinothk.rxretrofit.bean.PageVo
import kotlinx.android.synthetic.main.comm_activity_load_recycle_view_load_tip_shadow.*
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class YunKeTangStudyRecordListActivity : AppEventBusRecycleViewTitleBaseActivity<YunKeVo>(), OnSuperListener<YunKeBean> {

    var viewModel: BizViewModel? = null
    private var adapter: LiangDuYunKeTangListAdapter? = null

    override fun getLineDrawable(): Int {
        return R.drawable.list_divider_none
    }

    override fun getLayoutResId(): Int = R.layout.comm_activity_load_recycle_view_load_tip_shadow

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setViewTitle("我的在线学习")

        viewModel = BizViewModel()

        initView()
    }

    private fun initView() {
        initRecycleGridView(loadingRecyclerView, 30, 2, 0)

        adapter = LiangDuYunKeTangListAdapter(this)
        adapter!!.setOnSuperListener(this)
        loadingRecyclerView.adapter = adapter
        loadingRecyclerView.setLoadingMoreEnabled(false)

        refreshData()
        loadingTipView.showLoading("努力加载中")

        loadingTipView.setRetryListener {
            loadingTipView.showLoading("努力加载中")
            refreshData()
        }
    }

    override fun loadData(pageVo: PageVo<YunKeVo>) {
        val pageReq = PageReq<YunKeVo>()
        pageReq.pageNo = pageVo.pageNum
        pageReq.pageSize = pageVo.pageSize
        pageReq.data = YunKeVo()
        viewModel!!.getMyYunkePageData(pageReq)
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public fun eventBusCallback(result: ResultInfo<AppPageData<YunKeRecordBean>>) {

        if ("getMyYunkePageData" != result.eventType) {
            return
        }

        stopLoading(loadingRecyclerView, loadType)

        when (result.code) {
            ResultInfo.SUCCESS -> {
                if (result.data != null) {
                    setPageData(result.data)
                } else {
                    loadingTipView.showError("数据异常")
                }
            }
            ResultInfo.TOKEN_OVERDUE -> {
                loadingTipView.showError("Token过期，请重新登录")
            }
            else -> {
                loadingTipView.showError(result.msg)
            }
        }
    }

    private fun setPageData(appPageData: AppPageData<YunKeRecordBean>) {
        if (loadType == LoadType.REFRESH) {
            if (appPageData.records == null || appPageData.records.size == 0) {
                loadingTipView.showEmpty("暂无数据", R.drawable.empty_nodata)
                loadingRecyclerView.setLoadingMoreEnabled(false)
            } else {

                val refreshList = ArrayList<YunKeBean>()
                for (recordBean in appPageData.records) {
                    val tempBean = YunKeBean()
                    tempBean.name = recordBean.mmName
                    tempBean.imgUrl = recordBean.imgUrl
                    tempBean.id = recordBean.learnId
                    tempBean.learningInfo = recordBean.learningInfo
                    tempBean.integral = recordBean.integral

                    refreshList.add(tempBean)
                }

                adapter!!.setData(refreshList)
                loadingTipView.showContent()
            }
        } else {
            if (appPageData.records == null || appPageData.records.size == 0) {
                tip("没有更多数据了")
            } else {
                val moreList = ArrayList<YunKeBean>()
                for (recordBean in appPageData.records) {
                    val tempBean = YunKeBean()
                    tempBean.name = recordBean.mmName
                    tempBean.imgUrl = recordBean.imgUrl
                    tempBean.id = recordBean.learnId
                    tempBean.learningInfo = recordBean.learningInfo
                    tempBean.integral = recordBean.integral

                    moreList.add(tempBean)
                }

                adapter!!.updateData(moreList)
                loadingTipView.showContent()
            }
        }

        // 设置：通用
        if (appPageData.current < appPageData.pages) {
            loadingRecyclerView.setLoadingMoreEnabled(true)
        } else {
            loadingRecyclerView.setLoadingMoreEnabled(false)
            loadingRecyclerView.setNoMore(true)
        }
    }

    override fun onClick(position: Int, itemData: YunKeBean, flag: String) {
        XUtils.intent().openActivity(this, YunKeTangClassListActivity::class.java)
            .putSerializableExtra("yunKeInfo", itemData)
            .start()
    }
}